package com.yun.lib.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public abstract class BaseFragment extends Fragment {
    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (setLayoutResourceId() != 0) {
            View view = inflater.inflate(setLayoutResourceId(), container, false);
            context = getContext();
            initViews(savedInstanceState, view, inflater);
            initVariables();
            loadData();
            return view;
        } else return null;
    }

    /**
     * @return 布局文件
     */
    protected abstract int setLayoutResourceId();

    /**
     * 初始化变量，其中包括intent传递的参数
     */
    protected void initVariables() {
    }

    /**
     * @param savedInstanceState 初始化控件，并设置事件方法
     */
    protected abstract void initViews(Bundle savedInstanceState, View view, LayoutInflater inflater);

    /**
     * 加载数据
     */
    protected void loadData() {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public void showToast(String msg) {

        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

    }

    public void goNext(String name, Class c) {
        Intent i = new Intent(context, c);
        i.putExtra("title", name);
        startActivity(i);
    }

}
